package com.xindz.cn.mvptest.components.user.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVGeoPoint;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.logger.Logger;
import com.xindz.cn.mvptest.R;
import com.xindz.cn.mvptest.base.BaseActivity;
import com.xindz.cn.mvptest.components.user.data.LZUser;
import com.xindz.cn.mvptest.components.user.persenter.RegisterPersenter;
import com.xindz.cn.mvptest.shared.core.glide.GlideApp;
import com.xindz.cn.mvptest.shared.core.glide.GlideRequests;
import com.xindz.cn.mvptest.shared.core.image.ImageLoader;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name MvpTest
 * @class name：com.xindz.cn.mvptest.components.user.view
 * @describe describe
 * @anthor XINDZ
 * @time 2017/10/27 15:14
 * @change
 * @chang time
 * @class describe
 */
public class RegisterActivity extends BaseActivity<RegisterView, RegisterPersenter> implements RegisterView {
    @BindView(R.id.et_user_num)
    TextInputEditText etUserNum;
    @BindView(R.id.tiet_user_pwd)
    TextInputEditText tietUserPwd;
    @BindView(R.id.bt_user_register)
    Button btUserRegister;
    @BindView(R.id.bt_user_tologin)
    Button btUserTologin;
    @BindView(R.id.tv_user_register_show_prd)
    TextView tvUserRegisterShowPrd;
    @BindView(R.id.iv_user_register_show_prd)
    ImageView ivUserRegisterShowPrd;
    private String userNum;
    private String userPwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_register);
        ButterKnife.bind(this);
    }

    @Override
    public RegisterPersenter createPresneter() {
        return new RegisterPersenter();
    }

    @Override
    public RegisterView createView() {
        return this;
    }


    @OnClick({R.id.bt_user_register, R.id.bt_user_tologin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_user_register:
                LZUser.getCurrentUser().logOut();
                getString();
                getPresneter().register(userNum, userPwd);
                break;
            case R.id.bt_user_tologin:
                getString();
                getPresneter().login(userNum, userPwd);
                break;
        }
    }

    private void getString() {
        userNum = etUserNum.getEditableText().toString();
        userPwd = tietUserPwd.getEditableText().toString();
    }


    @Override
    public void toRegisterResult(AVException e) {
        if (e != null) ToastUtils.showShort("注册失败:" + e.getMessage());
        else ToastUtils.showShort("注册成功");
    }

    @Override
    public void toLoginResult(LZUser lzUser, AVException e) {
        if (e != null) {
            ToastUtils.showShort("登录失败:" + e.getMessage());
        } else {
            AVObject product = lzUser.getAVObject("Product");
            String objectId = product.getObjectId();
            getPresneter().getProduct(objectId);
            AVGeoPoint location = lzUser.getAVGeoPoint("location");
            Logger.i(location.getLatitude() + "");
            Logger.i(lzUser.toString());
        }
    }

    @Override
    public void getProduct(AVObject avObject, AVException e) {
        Logger.i("数据请求");
        if (e == null) {
            String title = avObject.getString("title");
            String description = avObject.getString("description");
            int price = avObject.getInt("price");
            LZUser owner = avObject.getAVUser("owner", LZUser.class);
            AVFile image = avObject.getAVFile("image");
            String xindz = avObject.getString("xindz");
            if (owner != null) {
                Logger.i(owner.getXinMing());
            }
            if (image != null) {
                String url = image.getUrl();
                ImageLoader.getInstance(GlideApp.with(RegisterActivity.this)).defaultLoad(url).into(ivUserRegisterShowPrd);
            }
            Logger.i(title + "\n" + description + "\n" + price + "\n" + xindz);
            Logger.i(avObject.toString());
        }
    }
}
